//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/bad_event/response/event_equipment_list_data.dart';
import 'package:ls_work/bad_event/ui/event_archiving_sheet.dart';
import 'package:saas_order_meal/model/combo_model.dart';
import 'package:saas_order_meal/net/net_response/code_value_list.dart';

import '../../util/ext_utils.dart';
import '../bad_event_model.dart';
import '../response/event_detail_data.dart';
import '../ui/elder_fall_event_page.dart';

class FallEventDetailViewModel extends BaseViewModel {
  dynamic eventId;
  List<EventEquipmentListData> equipmentList = [];

  FallEventDetailViewModel(BuildContext? context, {required this.eventId})
      : super(context);
  var model = BadEventModel();
  EventDetailData? eventDetail;

  var commonModel = ComboModel();

  Map<String, List<CodeValueList>> comboListMap = {};

  Future<void> getDetailInfo() async {
    showLoadingState();
    try {
      await getComboData('400004');
      await getComboData('97');
      await getComboData('96');
      await getComboData('44263');
      await getComboData('5878');
      await getComboData('4319');
      await getComboData('400006');
      await getComboData('400007');
      await getComboData('60');
      await getEquipmentList();
      eventDetail = await model.eventDetail(id: eventId);
      showContent();
    } catch (e) {
      print(e);
      showErrorState(e.toNetError());
    }
  }

  Future<void> getComboData(String code) async {
    comboListMap[code] = await commonModel.getComboData(code);
  }

  Future<void> getEquipmentList() async {
    equipmentList = await model.eventEquipment();
  }

  @override
  void onCreated() {
    super.onCreated();
    getDetailInfo();
  }

  Future<void> toChangePage() async {
    var data = await Go().push(ElderFallEventPage(eventId: eventDetail?.id,));
    if(data!=null){
      getDetailInfo();
    }
  }

  Future<void> eventArchiving() async {
    var data = await showEventArchivingSheet(context, id: eventDetail?.id);
    if(data!=null){
      getDetailInfo();
    }
  }

  void cancelEvent() {
    showApplyDialog(context: context, onSubmit: () async {
      showLoadingDialog(msg: '提交中...');
      try{
        await model.eventCancel(id: eventDetail?.id);
        hideDialog();
        showToast("作废成功");
        getDetailInfo();
      }catch(e){
        showToast(e.toNetError());
        hideDialog();
      }
    },message: '确定进行作废吗？',title: '提示');
  }


}
